package com.lightsaber.trade.as.web.endorsement;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.as.entity.category.Category;
import com.lightsaber.trade.as.entity.planning.Level1Planning;
import com.lightsaber.trade.as.entity.planning.Planning;
import com.lightsaber.trade.as.entity.planning.PlanningStatus;
import com.lightsaber.trade.as.service.planning.FirstLevelEndorseManager;
import com.lightsaber.trade.as.service.planning.PlanningService;
import com.lightsaber.trade.core.common.treegrid.GridConstants;
import com.lightsaber.trade.core.common.treegrid.GridDoc;
import com.lightsaber.trade.core.web.CrudActionSupport;

@Namespace("/endorsement")
public class SelectFirstLevelToEndorseAction extends CrudActionSupport<Level1Planning> {
    private final static Logger LOG = LoggerFactory
            .getLogger(SelectFirstLevelToEndorseAction.class);

    private static final long serialVersionUID = 3894011756180097226L;
    public static final String ENDORSE_MAP = "ENDORSE_MAP";

    @Autowired
    private transient FirstLevelEndorseManager firstLevelEndorseManager;

    @Autowired
    private PlanningService planningService;

    private String planningId;
    private String treeGridData;
    private Category category;
    private String planningStatus;

    @Override
    public Level1Planning getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        Planning planning = planningService.get(planningId);
        planningStatus = planning.getStatus().toString();

        return SUCCESS;
    }

    public String data() throws Exception {
        Planning planning = planningService.get(planningId);

        if (planning.getStatus() == PlanningStatus.Endorsed) {

            treeGridData = firstLevelEndorseManager.generateSelectionTree(planningId, null);

        } else {

            Map<String, String> endorseMap = getEndorseMap();

            treeGridData = firstLevelEndorseManager.generateSelectionTree(planningId, endorseMap);

        }

        Struts2Utils.renderXml(treeGridData);
        return null;

    }

    public void saveData() throws Exception {
        Map<String, String> endorseMap = getEndorseMap();

        LOG.info("XML = {}", treeGridData);
        Iterator<Element> ite = null;

        try {
            ite = GridDoc.parseChanges(treeGridData);
        } catch (DocumentException e) {
            LOG.error(e.getMessage(), e);
            Struts2Utils.renderXml(e.getMessage());
            return;
        }

        while (ite.hasNext()) {
            Element elemI = ite.next();

            if (!StringUtils.equals("1", elemI.attributeValue("Changed"))) {
                continue;
            }

            String id = elemI.attributeValue("id");
            String planName = elemI.attributeValue("C");
            LOG.info("change: nodeId={}, planNme={}", id, planName);

            endorseMap.put(id, planName);

        }

        Struts2Utils.renderXml(GridConstants.SUCESSFULLY);
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    public String getPlanningId() {
        return planningId;
    }

    public void setPlanningId(final String planningId) {
        this.planningId = planningId;
    }

    public String getTreeGridData() {
        return treeGridData;
    }

    public void setTreeGridData(final String treeGridData) {
        this.treeGridData = treeGridData;
    }

    private Map<String, String> getEndorseMap() {
        Map<String, String> endorseMap = (Map<String, String>) this.session.get(ENDORSE_MAP);

        if (endorseMap == null) {
            endorseMap = new HashMap<String, String>();
            this.session.put(ENDORSE_MAP, endorseMap);
        }

        return endorseMap;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(final Category category) {
        this.category = category;
    }

    public String getPlanningStatus() {
        return planningStatus;
    }

    public void setPlanningStatus(final String planningStatus) {
        this.planningStatus = planningStatus;
    }

}
